var gulp = require('gulp');
var concat = require('gulp-concat');
var sass = require('gulp-sass');
var minifyCss = require('gulp-minify-css');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');


var bower_components_path = './bower_components/**';
var lib_path = './www/lib';

var sassPath = './www/lib/zhx-calendar/sass/*.scss';
var sassPlugin = './www/lib/zhx-calendar/sass/zhx-base-cal.scss';

var demoSass = './www/css/style.scss';

//sass to css and minify
gulp.task('sass', function(done) {
  gulp.src([sassPlugin])
    .pipe(sass())
    .on('error', sass.logError)
    .pipe(gulp.dest('./www/lib/zhx-calendar/dist/'))
    .pipe(minifyCss({
      keepSpecialComments: 0
    }))
    .pipe(rename({ extname: '.min.css' }))
    .pipe(gulp.dest('./www/lib/zhx-calendar/dist/'))
    .on('end', done);
});

gulp.task('styleSass', function(done) {
  gulp.src([demoSass])
    .pipe(sass())
    .on('error', sass.logError)
    .pipe(gulp.dest('./www/css/'))
    .pipe(minifyCss({
      keepSpecialComments: 0
    }))
    .pipe(rename({ extname: '.min.css' }))
    .pipe(gulp.dest('./www/css/'))
    .on('end', done);
});

//make js concat to one js
gulp.task('script', function(){
  gulp.src( './www/lib/zhx-calendar/*.js' )
      .pipe( uglify() )
      .pipe( rename({ extname: '.min.js' } ) )
      .pipe( gulp.dest('./www/lib/zhx-calendar/dist/') );
});

//copy bower_components to www/lib
gulp.task('makeDpc',function(){
  gulp.src(bower_components_path)
    .pipe(gulp.dest(lib_path));
});

gulp.task('watch', function() {
  gulp.watch(sassPath, ['sass']);
  gulp.watch(demoSass, ['styleSass']);
});

